#!/usr/bin/env python
# encoding: utf-8
"""
extract.py

Created by Grégoire Détrez on 2009-02-22.
Copyright (c) 2009 __MyCompanyName__. All rights reserved.
"""

CANDIDATS_CLAUSES = ["Ssub", "VPpart", "VPinf", "COORD"] 

from tests import est_connecteur
from  ftbexplorer.fnd.linguistique import ConnectorClause, Clause, EmptyClause, WordClause

def Sentence2Clause(sentence):
    return TreeNode2Clause(sentence.tree)

def TreeNode2Clause(node):
    if node.is_word():
        if est_connecteur(node.word()):
            return ConnectorClause(word=node.word())
        else:
            return WordClause(word=node.word())
    fnd = EmptyClause()
    for c in node.children():
        fnd += TreeNode2Clause(c)
    if node.nodeName in CANDIDATS_CLAUSES and fnd.has_free_connector():
        fnd = Clause(fnd)
    return fnd

